<template>
  <div class="login-container">
    <van-form ref="form" @submit="handleSubmit">
      <!-- 使用 CSS 背景图像显示 Logo -->
      <div class="logo-container"></div>
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="请输入用户名"
        :rules="[{ required: true, message: '请输入用户名' }]"
        class="custom-field"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请输入密码' }]"
        class="custom-field"
      />
      <div class="button-container">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          class="submit-button"
          :loading="loading"
        >
          登录
        </van-button>
      </div>
      <div class="link-container">
        <router-link to="/register">没有账号？去注册</router-link>
        <router-link to="/forgot-password">忘记密码？</router-link>
      </div>
    </van-form>
    <!-- 加载指示器 -->
    <div v-if="loading" class="loading-overlay">
      <van-loading size="40px" color="#07c160" vertical>
        正在加载...
      </van-loading>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { Toast } from "vant";
import { useUserStore } from "@/store/userStore"; // 假设你有一个用户存储
import { userLoginService } from "@/api/user"; // 假设你有一个登录服务

const form = ref(null);
const username = ref("");
const password = ref("");
const loading = ref(false);
const router = useRouter();
const userStore = useUserStore();

const handleSubmit = async () => {
  try {
    // if (!(await validateForm())) return;

    loading.value = true;
    const loginResult = await userLoginService({
      username: username.value,
      password: password.value,
    });
    handleLoginResponse(loginResult);
  } catch (error) {
    //handleError(error);
  } finally {
    loading.value = false;
  }
};

const validateForm = async () => {
  try {
    return await form.value.validate();
  } catch {
    Toast.fail("表单验证失败");
    return false;
  }
};

const performLogin = async () => {
  return await userLoginService({
    accountName: username.value,
    password: password.value,
  });
};

const handleLoginResponse = (result) => {
  if (result.code === 1) {
    userStore.setUser(result.data);
    router.push("/layout");
    Toast.success(result.message || "登录成功");
  } else {
    Toast.fail(result.message || "登录失败");
  }
};

// const handleError = (error) => {
//   console.error("Error during login:", error.response?.data || error.message);
//   Toast.fail("服务错误");
// };
</script>

<style scoped>
.login-container {
  background-color: #f0f9eb;
  padding: 40px;
  border-radius: 16px;
  max-width: 400px;
  margin: auto;
  margin-top: 100px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease-in-out;
}

.login-container:hover {
  transform: scale(1.02);
}

.logo-container {
  width: 100%;
  height: 100px; /* 根据你的 Logo 大小调整 */
  background: url("@/assets/logo.png") no-repeat center center;
  background-size: contain;
  margin-bottom: 30px;
}

h2 {
  color: #07c160;
  text-align: center;
  font-size: 2rem;
  margin-bottom: 30px;
}

.button-container {
  margin: 20px 0;
  text-align: center;
}

.submit-button {
  background-color: #07c160;
  border: none;
  color: white;
  padding: 12px 24px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s ease-in-out, transform 0.3s ease-in-out;
}

.submit-button:hover {
  background-color: #05a64d;
  transform: translateY(-5px);
}

.link-container {
  display: flex;
  justify-content: space-around;
  margin-top: 20px;
}

.link-container a {
  color: #07c160;
  text-decoration: none;
  font-size: 0.9rem;
  transition: color 0.3s ease-in-out;
}

.link-container a:hover {
  color: #05a64d;
}

/* 美化输入框 */
.custom-field {
  border: 2px solid #ced4da;
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 16px;
  background-color: #fff;
  transition: border-color 0.3s ease-in-out, box-shadow 0.3s ease-in-out;
}

.custom-field:focus-within {
  border-color: #07c160;
  box-shadow: 0 0 0 0.2rem rgba(7, 193, 96, 0.25);
}

.custom-field .van-field__label {
  color: #495057;
  font-weight: 500;
}

.custom-field .van-field__control::placeholder {
  color: #adb5bd;
  font-size: 0.9rem;
}

/* 加载指示器样式 */
.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}
</style>